package com.meshnetics.gb.util;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * Class for managing byte buffer order usage.
 *
 * @author Yury Aserchev (yaserchev@meshnetics.com)
 *         Date: 25.09.2008 14:40:43
 */
public final class ByteBufferManager {
    private static final ByteOrder ORDER = ByteOrder.LITTLE_ENDIAN;

    /**
     * Allocates byte buffer with current used byte order.
     * @param size size of buffer.
     * @return allocated buffer.
     */
    public static ByteBuffer allocate(final int size) {
        return ByteBuffer.allocate(size).order(ORDER);
    }

    /**
     * Wrap given array to byte buffer with current used byte order.
     * @param array given array.
     * @return result buffer.
     */
    public static ByteBuffer wrap(final byte[] array) {
        return ByteBuffer.wrap(array).order(ORDER);
    }

    /**
     * Allocates byte buffer with BIG_ENDIAN byte order.
     * @param size size of buffer.
     * @return allocated buffer.
     */
    public static ByteBuffer allocateBE(final int size) {
        return allocate(size).order(ByteOrder.BIG_ENDIAN);
    }

    /**
     * Wrap given array to byte buffer with BIG_ENDIAN byte order.
     * @param array given array.
     * @return result buffer.
     */
    public static ByteBuffer wrapBE(final byte[] array) {
        return wrap(array).order(ByteOrder.BIG_ENDIAN);
    }
}
